/*
 * 创建日期 2005-10-7
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.cfg;

import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import com.vanda.report.ReportException;
import com.vanda.report.ReportUtils;
import com.vanda.report.element.Element;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class ReportParse {
	private static Log log = LogFactory.getLog(ReportParse.class);
	private static final String[] SUBPACKAGE = {"view","dp","datas"};
	
	public static List parseXML(String resource,URI dir){
		Element element = null;
		List lstElement = new ArrayList();
		try{
			InputStream stream = ReportUtils.getMappingStream(resource,dir);
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(stream);
			
			org.dom4j.Element root = document.getRootElement();
			Iterator iter = root.elementIterator();
			while(iter.hasNext()){
				org.dom4j.Element child = (org.dom4j.Element)iter.next();
				element = parse(child,null);
				lstElement.add(element);
			}
			

		}catch(Exception e){
			e.printStackTrace();
		}
		
		return lstElement;
	}
	
	private static Element parse(org.dom4j.Element element,String subPackage) throws Exception{
		String name = element.getName();
		if(isIn(name)){
			subPackage = name;
		}
		
		Element elt = Element.getInstance(subPackage,element.getName());
		
		List subElement = new ArrayList();
		elt.setSubElements(subElement);
		Iterator iter = element.attributeIterator();
		
		while(iter.hasNext()){
			Attribute attr = (Attribute)iter.next();
			String aname = attr.getName();
			Object avalue = attr.getData();
			//System.out.println("name = "+name+":value = "+value);
			setProperty(elt,name,aname,avalue);
		}
		
		//校验xml
		elt.validator();
		
		iter = element.elementIterator();
		while(iter.hasNext()){
			org.dom4j.Element child = (org.dom4j.Element)iter.next();
			
			Element childElt = parse(child,subPackage);
			
			subElement.add(childElt);
		}
		
		return elt;	
	}
	
	private static boolean isIn(String name){
		for(int i=0;i<SUBPACKAGE.length;i++){
			if(name.equals(SUBPACKAGE[i])){
				return true;
			}
		}
		return false;
	}
	
	private static void setProperty(Object element,String elementName,String property,Object value) throws ReportException{
		try{
			PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(element,property);
			if(desc == null){
				throw new ReportException("Element["+elementName+"],Atrribute["+property+"] is not exist!");
			}
		
			String clsName = desc.getPropertyType().getName();
		
			if(clsName.equals("int") || clsName.equals("java.lang.Integer")){
				value = new Integer(value.toString());	
			}else if(clsName.equals("boolean") || clsName.equals("java.lang.Boolean")){
				value = new Boolean(value.toString());
			}
			
		
			PropertyUtils.setProperty(element,property,value);
		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}

	}
	
	public static void main(String[] args) throws Exception{
		//test2();
	}
	
	/*private static void test1() throws Exception{
		com.vanda.report.ReportAccessor accessor = com.vanda.report.ReportAccessor.getInstance();
		ReportData reportData = getReportData();
		
		accessor.runReport("")
		HSSFWorkbook workbook = reportData.getWorkbook();
	
		workbook.write(new FileOutputStream("c:/2.xls"));
	}*/
	
	/*private static void test2() throws Exception{
		com.vanda.report.ReportAccessor accessor = com.vanda.report.ReportAccessor.getInstance();
		
		ReportData reportData = getReportData1();
		
		accessor.runReport("test1",reportData,"c:/test");
		
		HSSFWorkbook workbook = reportData.getWorkbook();
	
		workbook.write(new FileOutputStream("c:/Report_dp2.xls"));
	}
	
	private static ReportData getReportData(){
		ReportData reportData = new ReportData();
		
		SaleContractValueObject scvo = new SaleContractValueObject();
		scvo.setHwContractNo("111111111111");
		scvo.setContractDate(java.sql.Date.valueOf("2005-01-02"));
		reportData.setSelectData(scvo);
		
		List lstValue = new ArrayList();
		for(int i=0;i<10;i++){
			List subList = new ArrayList();
			for(int j=0;j<3;j++){
				scvo = new SaleContractValueObject();
				scvo.setHwContractNo("11111111111110"+i+j);
				scvo.setFrameContractNo("frame"+i+j);
				scvo.setContractDate(java.sql.Date.valueOf("2005-01-03"));
				subList.add(scvo);
			}
			lstValue.add(subList);
		}
		reportData.setReportList(lstValue);
		
		List listData1 = new ArrayList();
		for(int i=0;i<10;i++){
			scvo = new SaleContractValueObject();
			scvo.setHwContractNo("1111111111111dda"+i);
			listData1.add(scvo);
		}
		reportData.setData1(listData1);
		
		return reportData;
	}
	
	private static ReportData getReportData1(){
		ReportData reportData = new ReportData();
		
		SaleContractValueObject scvo = new SaleContractValueObject();
		scvo.setHwContractNo("111111111111");
		scvo.setContractDate(java.sql.Date.valueOf("2005-01-02"));
		reportData.setSelectData(scvo);
		
		List lstValue = new ArrayList();
		for(int i=0;i<10;i++){
			scvo = new SaleContractValueObject();
			scvo.setCountryID(1);
			scvo.setCompanyID(2);
			scvo.setVersionID(new Integer(100+i));
			scvo.setId(new Long(1000+i));
			scvo.setHwContractNo("11111111111110"+i);
			scvo.setFrameContractNo("frame"+i);
			scvo.setContractDate(java.sql.Date.valueOf("2005-01-03"));
			lstValue.add(scvo);
		}
		for(int i=0;i<5;i++){
			scvo = new SaleContractValueObject();
			scvo.setCountryID(1);
			scvo.setCompanyID(4);
			scvo.setVersionID(new Integer(100+i));
			scvo.setId(new Long(1000+i));
			scvo.setHwContractNo("11111111111110"+i);
			scvo.setFrameContractNo("frame"+i);
			scvo.setContractDate(java.sql.Date.valueOf("2005-01-03"));
			lstValue.add(scvo);
		}
		for(int i=0;i<5;i++){
			scvo = new SaleContractValueObject();
			scvo.setCountryID(4);
			scvo.setCompanyID(5);
			scvo.setVersionID(new Integer(100+i));
			scvo.setId(new Long(1000+i));
			scvo.setHwContractNo("11111111111110"+i);
			scvo.setFrameContractNo("frame"+i);
			scvo.setContractDate(java.sql.Date.valueOf("2005-01-03"));
			lstValue.add(scvo);
		}
		reportData.setReportList(lstValue);
		
		return reportData;
	}
	
	public void test(ReportInnerData reportData){
		SaleContractValueObject scvo = new SaleContractValueObject();
		scvo.setHwContractNo("22222222222");
		scvo.setContractDate(java.sql.Date.valueOf("2005-01-03"));
		reportData.setSelectData(scvo);
		
		System.out.println("eagle coming");
	}*/
}
